﻿using BITUI.Element;
using SDL2;
using System;
using System.Collections.Generic;
using System.Text;
using Rect = SDL2.SDL.SDL_Rect;

namespace BITUI.Tile
{
    public class RenderTile:IFillDraw
    {
        public Bank bank;
        public int tileID;
        public IntPtr tex;
        public Rect srcrect;
        public void FillDraw(IWindowRender render, int x, int y, int w, int h,byte colorReplace)
        {
            render.DrawTile(this, x, y, colorReplace, w / this.srcrect.w, h / this.srcrect.h);
        }

    }
    public class WriteableTile : RenderTile
    {
        public SDL.SDL_Color[] palette;
        internal WriteableTile(IntPtr sdlRender, SDL.SDL_Color[] palette, int width, int height)
        {
            this.palette = palette;
            this.tex = SDL.SDL_CreateTexture(sdlRender,
                SDL.SDL_PIXELFORMAT_ARGB8888,
                 (int)SDL.SDL_TextureAccess.SDL_TEXTUREACCESS_STREAMING,
                 width, height);
            //SDL_TEXTUREACCESS_STATIC 贴图适合一次创建，不再改动，我们需要经常修改，用streaming

            SDL.SDL_SetTextureBlendMode(this.tex, SDL.SDL_BlendMode.SDL_BLENDMODE_BLEND);
            this.tileID = -1;
            this.bank = null;
            this.srcrect.x = 0;
            this.srcrect.y = 0;
            this.srcrect.w = width;
            this.srcrect.h = height;
        }
        public void FixPixel(Action<WriteableTile, IntPtr, int> fixfunc)
        {
            int lockresult = SDL.SDL_LockTexture(this.tex, ref this.srcrect, out IntPtr pixels, out int pitch);
            fixfunc(this, pixels, pitch);
            SDL.SDL_UnlockTexture(this.tex);
        }
        public void Free()
        {
            SDL.SDL_DestroyTexture(this.tex);
            this.tex = IntPtr.Zero;
        }
    }
}
